<template>
  <div ref="getheight" class="wrap">
    <el-container>
      <el-main>
        <div class="table-wrap">
          <div class="handle-box3">
            <el-form :inline="true" ref="form" class="demo-form-inline">
              <div class="uers-right">
                <el-popover placement="bottom" title="批量导入" width="220" trigger="click">
                  <div>
                    <el-upload
                      class="upload-demo"
                      action="https://jsonplaceholder.typicode.com/posts/"
                      :on-success="handleAvatarSuccess"
                      :auto-upload="true"
                      :on-change="uploadChange"
                      multiple
                      accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                      >
                      <el-button size="small" type="primary">选择导入模板</el-button>
                    </el-upload>
                    <!-- <input
                      class="file-up"
                      type="file"
                      @change="importExcel(this)"
                      accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                    />
                    <el-button type="primary">选择导入模板</el-button> -->
                    
                    <el-button type="success" plain icon="el-icon-download" class="down-template">下载模板</el-button>
                  </div>
                  <p class="import-text">只能选择 .xls .xlsx 文件</p>
                  <div>
                    <el-button type="success" @click="importData">开始导入</el-button>
                  </div>
                  <el-button type="primary" plain icon="el-icon-upload2" slot="reference">导入实体票</el-button>
                </el-popover>
              </div>
            </el-form>
          </div>
          <div class="tabbox">
          <el-table
            :data="dataList"
            height="100%"
            border
            class="tabbox"
            ref="multipleTable"
            :header-cell-style="{background:'#f2f6fc',color:'#606266'}"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center"></el-table-column>
            <el-table-column type="index" label="序号" align="center" width="60"></el-table-column>
            <el-table-column prop="codeValue" label="码值" align="center">
              <template slot-scope="scope">
                <span>{{scope.row.codeValue}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="ticketNumber" label="票号" align="center">
              <template slot-scope="scope">
                <span>{{scope.row.ticketNumber}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="type" label="证件类型" align="center">
              <template slot-scope="scope">
                <span>{{scope.row.type}}</span>
              </template>
            </el-table-column>
          </el-table></div>
          <div class="fb"><el-pagination
            class="pag"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            background
          ></el-pagination></div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      total: 10, //总条数
      currentPage: 1, //初始页
      pagesize: 5, // 每页的数据
      totalPage: 0,
      dataList: [],
      arrayList:[],
      multipleSelection: []
    };
  },
  created() {
    this.getData();
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      // this.getuserlist();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      console.log(`当前页: ${val}`);
      // this.getuserlist();
    },
    deleteData() {},
    handleCloseVisible() {
      this.delVisible = false;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 获取数据
    getData() {
       this.dataList = this.dataList;
    },
    handleAvatarSuccess() { 
    },
    uploadChange(file, fileList){
      let inputDOM = this.$refs.inputer; // 通过DOM取文件数据

      this.file = event.currentTarget.files[0];
      var rABS = false; //是否将文件读取为二进制字符串
      var f = this.file;

      var reader = new FileReader(); //if (!FileReader.prototype.readAsBinaryString) {

      FileReader.prototype.readAsBinaryString = function(f) {
        var binary = "";
        var rABS = false; //是否将文件读取为二进制字符串
        var pt = this;
        var wb; //读取完成的数据
        var outdata;
        var reader = new FileReader();
        reader.onload = function(e) {
          var bytes = new Uint8Array(reader.result);
          var length = bytes.byteLength;
          for (var i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i]);
          }
          var XLSX = require("xlsx");
          if (rABS) {
            wb = XLSX.read(btoa(fixdata(binary)), {
              //手动转化
              type: "base64"
            });
          } else {
            wb = XLSX.read(binary, {
              type: "binary"
            });
          } // outdata就是你想要的东西 excel导入的数据

          outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); // excel 数据再处理
          let arr = [];

          outdata.map(v => {
            let obj = {};
            obj.codeValue = v.码值;
            obj.ticketNumber = v.票号;
            obj.type = v.证件类型;
            // // obj.id = v.id;
            arr.push(obj);
          });

          this.arrayList = [...arr];
          // _this.reload();
          localStorage.setItem('arrayList',JSON.stringify(this.arrayList))
        };
       
        reader.readAsArrayBuffer(f);
      };

      if (rABS) {
        reader.readAsArrayBuffer(f);
      } else {
        reader.readAsBinaryString(f);
      }
    },

    importData(){
      var array = JSON.parse(localStorage.getItem('arrayList'));
      this.dataList=array;
    }
  }
};
</script>
<style lang="scss" scope>
element.style {
  left: -20px !important;
  right: 10px !important;
}
.wrap {
  box-sizing: border-box;
  padding: 0 10px;
  border-radius: 5px;
  position: relative;
  height: 100%;
  background: #fff;
  .pag {
    position: absolute;
    bottom: 0;
  }
  .table-wrap {
    padding-top: 20px;
  }
}
.uers-right {
  float: right;
  margin-bottom: 20px;
}
.file-up {
  width: 100px;
  position: absolute;
  height: 32px;
  opacity: 0;
}
.import-text {
  margin: 15px 0;
}
.el-upload--text{
  width: 110px;
  height: 32px;
  line-height: 32px;
  border: 0;
}
.down-template{
  position: absolute;
  top: 40px;
  right: 10px;
}
.el-dialog{height: auto;}
</style>